﻿using System;
using System.Text;
using System.Windows.Forms;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using RabbitMQ.Client.Framing;

namespace EasyMQ
{
    public partial class RoutingDirect : Form
    {
        private string exchange = "RoutingTest";


        private Action<string, TextBox> SetText;

        private static readonly RoutingDirect _mainForm;
        static RoutingDirect()
        {
            _mainForm = new RoutingDirect();
        }

        private RoutingDirect()
        {
            CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
            AcceptMessageWeb();
            AcceptMessageWeb1();
            AcceptMessageWeb2();
            AcceptMessageWeb3();
            SetText += OnSetText;
        }

        /// <summary>
        /// 主窗口单列
        /// </summary>
        public static RoutingDirect SingleForm
        {
            get { return _mainForm; }
        }


        private void button1_Click(object sender, EventArgs e)
        {
            SendMessageWeb("Info");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SendMessageWeb("Error");
        }

        #region 发送消息

        /// <summary>
        /// 发送一条消息(Web管理页面提前创建列队)
        /// </summary>
        public void SendMessageWeb(string key)
        {
            var connectionFactory = new ConnectionFactory { HostName = "localhost" };
            using (var connection = connectionFactory.CreateConnection())
            {
                using (var channel = connection.CreateModel())
                {
                    BasicProperties basicProperties = new BasicProperties { Persistent = true };
                    var payload = Encoding.UTF8.GetBytes(textBox1.Text);

                    channel.BasicPublish(exchange, key, basicProperties, payload);
                }
            }
        }

        #endregion

        /// <summary>
        /// 接受消息
        /// </summary>
        public void AcceptMessageWeb()
        {
            ConnectionFactory connectionFactory1 = new ConnectionFactory { HostName = "localhost", UserName = "twb", Password = "twb0223" };
            var connection = connectionFactory1.CreateConnection();
            var channel = connection.CreateModel();

            var consumer = new EventingBasicConsumer(channel);

            consumer.Received += (ch, deliver) =>
            {
                string jsonString = Encoding.UTF8.GetString(deliver.Body);
                textBox2.Invoke(SetText, jsonString, textBox5);
            };
            channel.BasicConsume("RoutingTest_Error_1", true, consumer);
        }

        /// <summary>
        /// 接受消息
        /// </summary>
        public void AcceptMessageWeb1()
        {
            ConnectionFactory connectionFactory1 = new ConnectionFactory { HostName = "localhost", UserName = "twb", Password = "twb0223" };
            var connection = connectionFactory1.CreateConnection();
            var channel = connection.CreateModel();

            var consumer = new EventingBasicConsumer(channel);

            consumer.Received += (ch, deliver) =>
            {
                string jsonString = Encoding.UTF8.GetString(deliver.Body);
                textBox3.Invoke(SetText, jsonString, textBox6);
            };
            channel.BasicConsume("RoutingTest_Error_2", true, consumer);
        }

        /// <summary>
        /// 接受消息
        /// </summary>
        public void AcceptMessageWeb2()
        {
            ConnectionFactory connectionFactory1 = new ConnectionFactory { HostName = "localhost" };
            var connection = connectionFactory1.CreateConnection();
            var channel = connection.CreateModel();

            var consumer = new EventingBasicConsumer(channel);

            consumer.Received += (ch, deliver) =>
            {
                string jsonString = Encoding.UTF8.GetString(deliver.Body);
                textBox2.Invoke(SetText, jsonString, textBox2);
            };
            channel.BasicConsume("RoutingTest_Info_1", true, consumer);
        }

        /// <summary>
        /// 接受消息
        /// </summary>
        public void AcceptMessageWeb3()
        {
            ConnectionFactory connectionFactory1 = new ConnectionFactory { HostName = "localhost" };
            var connection = connectionFactory1.CreateConnection();
            var channel = connection.CreateModel();

            var consumer = new EventingBasicConsumer(channel);

            consumer.Received += (ch, deliver) =>
            {
                string jsonString = Encoding.UTF8.GetString(deliver.Body);
                textBox3.Invoke(SetText, jsonString, textBox3);
            };
            channel.BasicConsume("RoutingTest_Info_2", true, consumer);
        }

        public void OnSetText(string text, TextBox textBox)
        {
            textBox.Text += string.Format("{0}\r\n", text);
        }

     
    }
}
